package com.sales.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hanxi on 2017/9/16.
 */
public class SalesRequestLoggingFilter extends CommonsRequestLoggingFilter {
    private static Logger LOG = LoggerFactory.getLogger(SalesRequestLoggingFilter.class);

    public SalesRequestLoggingFilter() {
        super();
        this.setIncludePayload(true);
        this.setMaxPayloadLength(1024);
        this.setIncludeHeaders(true);
        this.setIncludeQueryString(true);
    }

    @Override
    protected boolean shouldLog(HttpServletRequest request) {
        return true;
    }

    @Override
    protected void beforeRequest(HttpServletRequest request, String message) {
        MDC.put("session_id", request.getSession().getId());
        LOG.info(message);

    }

    @Override
    protected void afterRequest(HttpServletRequest request, String message) {
        LOG.info(message);
        MDC.clear();
    }
}
